package com.vr.xxaiagent.chatmemory.converter;

import com.vr.xxaiagent.model.entity.ChatMessage;
import org.springframework.ai.chat.messages.*;

import java.util.Collections;
import java.util.Map;

/**
 * 消息转换器
 *
 * @author hzh
 * @date 2025/06/02
 */
public class MessageConverter {

    /**
     * message转换为ChatMessage
     */
    public static ChatMessage toChatMessage(Message message, String conversationId) {
        return ChatMessage.builder()
                .conversationId(conversationId)
                .messageType(message.getMessageType())
                .textContent(message.getText())
                .metadata(message.getMetadata())
                .build();
    }

    /**
     * chatMessage转为message
     */
    public static Message toMessage(ChatMessage chatMessage) {
        MessageType messageType = chatMessage.getMessageType();
        Map<String, Object> metadata = chatMessage.getMetadata();
        String textContent = chatMessage.getTextContent();
        // 不同类型生成的消息不一样
        return switch (messageType) {
            case USER -> new UserMessage(textContent);
            case ASSISTANT -> new AssistantMessage(textContent, metadata);
            case SYSTEM -> new SystemMessage(textContent);
            case TOOL -> new ToolResponseMessage(Collections.emptyList(), metadata);
        };
    }
}
